﻿<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.2//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${basePackage}.dao${m.prefixPoint(table.modelNames[0])}.${table.className}Mapper">

    <sql id="TABLE">${table.tableName}</sql>
    <sql id="QUERY_COLUMN_LIST">
    <#list table.columns as column>
    <#if column.fieldType == "Date">
        ${column.columnName} AS ${column.fieldNameFirstLower},
        DATE_FORMAT(${column.columnName},'%Y-%m-%d %H:%i:%s') AS ${column.fieldNameFirstLower}Str<#if column_has_next>,</#if>
    <#else>
        ${column.columnName} AS ${column.fieldNameFirstLower}<#if column_has_next>,</#if>
    </#if>
    </#list>
    </sql>
    <!-- all condition -->
    <sql id="QUERY_WHERE">
        <where>
        <#list table.columns as column>
        <#if column.fieldType == "String">
            <if test='${column.fieldNameFirstLower} != null and ${column.fieldNameFirstLower} !=""'><![CDATA[AND ${column.columnName} = <#noparse>#</#noparse>{${column.fieldNameFirstLower}}]]></if>
        <#else>
            <if test='${column.fieldNameFirstLower} != null'><![CDATA[AND ${column.columnName} = <#noparse>#</#noparse>{${column.fieldNameFirstLower}}]]></if>
        </#if>
        </#list>
            <if test="idNotEqualTo != null"><![CDATA[AND id != <#noparse>#</#noparse>{idNotEqualTo}]]></if>
        </where>
    </sql>

    <select id="count" parameterType="${basePackage}.domain${m.prefixPoint(table.modelName)}.${table.className}Vo" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM <include refid="TABLE"/>
        <include refid="QUERY_WHERE"/>
    </select>

    <select id="queryById" resultType="${basePackage}.domain${m.prefixPoint(table.modelName)}.${table.className}Vo">
        SELECT <include refid="QUERY_COLUMN_LIST"/> FROM <include refid="TABLE"/> WHERE id = <#noparse>#</#noparse>{id}
    </select>

    <select id="queryForObject" resultType="${basePackage}.domain${m.prefixPoint(table.modelName)}.${table.className}Vo">
        SELECT
        <include refid="QUERY_COLUMN_LIST"/>
        FROM <include refid="TABLE"/>
        <include refid="QUERY_WHERE"/>
        LIMIT 1
    </select>

    <select id="queryForList" resultType="${basePackage}.domain${m.prefixPoint(table.modelName)}.${table.className}Vo">
        SELECT
        <include refid="QUERY_COLUMN_LIST"/>
        FROM <include refid="TABLE"/>
        <include refid="QUERY_WHERE"/>
        ORDER BY id DESC
    </select>

    <insert id="insert" parameterType="${basePackage}.domain${m.prefixPoint(table.modelName)}.${table.className}" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO <include refid="TABLE"/>
        (
        <#list table.columns as column>
            ${column.columnName}<#if column_has_next>,</#if>
        </#list>
        )VALUES(
        <#list table.columns as column>
            <#noparse>#</#noparse>{${column.fieldNameFirstLower}}<#if column_has_next>,</#if>
        </#list>
        )
    </insert>

    <update id="update" parameterType="${basePackage}.domain${m.prefixPoint(table.modelName)}.${table.className}">
        UPDATE <include refid="TABLE"/>
        <set>
            <#list table.columns as column>
            <if test="${column.fieldNameFirstLower} != null"> ${column.columnName} = <#noparse>#</#noparse>{${column.fieldNameFirstLower}},</if>
            </#list>
        </set>
        WHERE id = <#noparse>#</#noparse>{id}
    </update>
</mapper>